package eu.gegenleitner.esspacc.gui;

import java.awt.EventQueue;

import javax.swing.JFrame;

import java.awt.Color;
import java.awt.SystemColor;

import javax.swing.JTextField;

import java.awt.BorderLayout;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import eu.gegenleitner.esspacc.communication.HttpRequester;

import java.awt.Font;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.security.KeyPair;

public class MainWindow {

	private JFrame frmClient;
	private JTextField eMailTextField;
	private JTextArea userInfos;
	private JLabel lblGivenname;
	private JLabel lblSurname;
	private JLabel label;
	
	private JPanel panel;
	private JTextField tokenTextField;
	private JLabel lblTokenFromMail;
	private JTextField gNameTextField;
	private JTextField sNameTextField;
	
	public KeyPair keyPair;
	public HttpRequester hReq;
	private JButton btnSubmitToken;
	private JButton btnSubmitCertificateSigning;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow window = new MainWindow();
					window.frmClient.setVisible(true);
					/*TODO:no hardcoded URL*/
					window.hReq = new HttpRequester("http://localhost:8080/FBA_Server/Service");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainWindow() {
		initialize();
	}
	
	public void addLineToUserInfo(String nextLine, boolean withNewLine) {
		userInfos.setText(userInfos.getText() + (withNewLine ? "\n" : "") + nextLine);
	}
	
	public void endProgramm() {
		System.exit(0);
	}
	
	private void autoFillInUsername() {
		Thread thread = new Thread(new RunnableAutoFillInUserName(this));
		thread.start();
	}
	
	private void createCertificateSigningRequest() {
		if( !btnSubmitCertificateSigning.isEnabled() ) {
			return;
		}
		Thread thread = new Thread(new RunnableCertificateRequester(this));
		thread.start();
	}
	
	private void sendTokenToService() {
		if( !btnSubmitToken.isEnabled() ) {
			return;
		}
		Thread thread = new Thread(new RunnableTokenSender(this));
		thread.start();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmClient = new JFrame();
		frmClient.addWindowListener(new WindowAdapter() {
			@Override
			public void windowOpened(WindowEvent arg0) {
				autoFillInUsername();
			}
			@Override
			public void windowClosing(WindowEvent arg0) {
				hReq.close();
			}
		});
		frmClient.setResizable(false);
		frmClient.setForeground(SystemColor.window);
		frmClient.setTitle("Client");
		frmClient.setBackground(SystemColor.window);
		frmClient.setBounds(100, 100, 528, 364);
		frmClient.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		panel = new JPanel();
		panel.setBackground(SystemColor.window);
		frmClient.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		gNameTextField = new JTextField();
		gNameTextField.setEditable(false);
		gNameTextField.setBounds(154, 11, 261, 20);
		panel.add(gNameTextField);
		gNameTextField.setColumns(10);
		
		lblGivenname = new JLabel("Givenname");
		lblGivenname.setBounds(36, 14, 67, 14);
		panel.add(lblGivenname);
		
		lblSurname = new JLabel("Surname");
		lblSurname.setBounds(36, 45, 67, 14);
		panel.add(lblSurname);
		
		sNameTextField = new JTextField();
		sNameTextField.setEditable(false);
		sNameTextField.setBounds(154, 42, 261, 20);
		panel.add(sNameTextField);
		sNameTextField.setColumns(10);
		
		userInfos = new JTextArea();
		userInfos.setFont(new Font("Monospaced", Font.PLAIN, 10));
		userInfos.setText("############# Infos will be displayed here ################");
		userInfos.setForeground(Color.WHITE);
		userInfos.setToolTipText("");
		userInfos.setBackground(SystemColor.desktop);
		userInfos.setEditable(false);
		userInfos.setLineWrap(true);		
		JScrollPane scroll = new JScrollPane (userInfos);
		scroll.setBounds(10, 212, 500, 112);
		panel.add(scroll);
		
		label = new JLabel("E-Mail-Address");
		label.setBounds(36, 76, 119, 14);
		panel.add(label);
		
		eMailTextField = new JTextField();
		eMailTextField.setColumns(10);
		eMailTextField.setBounds(154, 73, 261, 20);
		panel.add(eMailTextField);
		
		btnSubmitCertificateSigning = new JButton("Submit Certificate Signing Request");
		btnSubmitCertificateSigning.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				createCertificateSigningRequest();
			}
		});
		btnSubmitCertificateSigning.setBackground(Color.WHITE);
		btnSubmitCertificateSigning.setBounds(154, 101, 261, 23);
		panel.add(btnSubmitCertificateSigning);
		
		lblTokenFromMail = new JLabel("Token from Mail");
		lblTokenFromMail.setBounds(36, 153, 119, 14);
		panel.add(lblTokenFromMail);
		
		tokenTextField = new JTextField();
		tokenTextField.setEditable(false);
		tokenTextField.setToolTipText("Format = XXXX-XXXX-XXXX-XXXX");
		tokenTextField.setColumns(10);
		tokenTextField.setBounds(154, 150, 261, 20);
		panel.add(tokenTextField);
		
		btnSubmitToken = new JButton("Submit Token");
		btnSubmitToken.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				sendTokenToService();
			}
		});
		btnSubmitToken.setEnabled(false);
		btnSubmitToken.setBackground(Color.WHITE);
		btnSubmitToken.setBounds(154, 178, 261, 23);
		panel.add(btnSubmitToken);
		
	}
	public JTextField getSNameTextField() {
		return sNameTextField;
	}
	public JTextField getGNameTextField() {
		return gNameTextField;
	}
	public JTextField getEMailTextField() {
		return eMailTextField;
	}
	public JTextField getTokenTextField() {
		return tokenTextField;
	}
	public JLabel getLblTokenFromMail() {
		return lblTokenFromMail;
	}
	public JButton getBtnSubmitToken() {
		return btnSubmitToken;
	}
	public JButton getBtnSubmitCertificateSigning() {
		return btnSubmitCertificateSigning;
	}
}
